package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Problem;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IProblemService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IProblemStatisVOService;
import cn.wolfcode.luowowo.cache.service.IUserInfoReplyVOService;
import cn.wolfcode.luowowo.cache.service.IUserInfoStaticVOService;
import cn.wolfcode.luowowo.cache.vo.ProblemStatisVO;
import cn.wolfcode.luowowo.cache.vo.UserInfoReplyVO;
import cn.wolfcode.luowowo.cache.vo.UserInfoStatisVO;
import cn.wolfcode.luowowo.comment.domain.ProblemComment;
import cn.wolfcode.luowowo.comment.query.ProblemCommentQuery;
import cn.wolfcode.luowowo.comment.service.IProblemCommentService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.member.vo.Fans;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import java.util.List;

/**
 * Created by Windows on 2020/1/11.
 */
@Controller
@RequestMapping("problem")
public class ProblemController {
    //问题服务
    @Reference
    private IProblemService problemService;

    //地点服务
    @Reference
    private IDestinationService destinationService;

    //主题服务
    @Reference
    private IStrategyTagService strategyTagService;

    //mongo的服务
    @Reference
    private IProblemCommentService problemCommentService;

    //用户服务
    @Reference
    private IUserInfoService userInfoService;

    //redis服务
    @Reference
    private IProblemStatisVOService problemStatisVOService;

    //用户的redis赞服务
    @Reference
    private IUserInfoStaticVOService userInfoStaticVOService;

    //用户的redis回答服务
    @Reference
    private IUserInfoReplyVOService userInfoReplyVOService;

    /**
     * 主页回显
     */
    @RequestMapping("")
    public String list(Model model){
        List<Problem> list = problemService.list();


        for (Problem problem : list) {
            //遍历list,查出他们所有的浏览数
            ProblemStatisVO vo = problemStatisVOService.getProblemStatisVO(problem.getId());
            //再设置到他们的浏览数中
            problem.setViewnum(vo.getViewnum());
            //遍历list,查出他们所有的点赞数
            ProblemStatisVO zvo = problemStatisVOService.getProblemStatisVOByZan(problem.getId());
            problem.setThumbsupnum(zvo.getThumbsupnum());
            //遍历list,查出他们所有的回答数
            ProblemStatisVO rvo = problemStatisVOService.getProblemStatisVOByReply(problem.getId());
            problem.setReplynum(rvo.getReplynum());
        }

        //查出赞的排名list
        List<UserInfoStatisVO> zanlist = userInfoStaticVOService.queryZan();
        model.addAttribute("zanlist",zanlist);
        //查出回答排名
        List<UserInfoReplyVO> replylist = userInfoReplyVOService.queryReply();
        model.addAttribute("replylist",replylist);



        model.addAttribute("problems",list);

        return "problem/wenda";

    }

    //跳转编辑页面
    @RequireLogin
    @RequestMapping("/input")
    public String input(@UserParam UserInfo userInfo, Model model){

        model.addAttribute("dests",destinationService.list());
        model.addAttribute("tags",strategyTagService.list());


        return "problem/input";
    }

    //问题添加
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(@UserParam UserInfo userInfo, Problem problem){

        if(userInfo==null){
            return new AjaxResult(false,"请先登录");
        }else{

            problem.setUser(userInfo);
        }

        problem.setId(userInfo.getId());
        problemService.save(problem);

        return new AjaxResult(true);

    }

    //跳转到问题明细界面
    @RequestMapping("/detail")
    public String detail(Model model,Long id,@UserParam UserInfo userInfo){

        //isFocus: 刷新页面后判断是否关注过的样式变化
        /*if(userInfo != null){
            model.addAttribute("isFocus", communityStaticVOService.hasFocus(cid, userInfo.getId()) ? 1: 2);
        }*/

        Problem problem = problemService.getById(id);
        model.addAttribute("p",problem);

        //关注人数
        Fans fans = new Fans();
        long count = userInfoService.getFansCount(problem.getUser().getId());
        fans.setFansCount(count);
        model.addAttribute("fans",fans);

        //问题的回答
        ProblemCommentQuery qo = new ProblemCommentQuery();
        qo.setProblemId(id);
        qo.setCurrentPage(1);
        qo.setPageSize(Integer.MAX_VALUE);
        Page page = problemCommentService.query(qo);
        List list = page.getContent();
        for (Object o : list) {
            ProblemComment comment = (ProblemComment) o;
            System.out.println(comment);
        }
        model.addAttribute("list",page.getContent());

        //redis 实现阅读数
        problemStatisVOService.increaseViewnum(id,1);
        model.addAttribute("vo",problemStatisVOService.getProblemStatisVO(id));


        return "problem/wendaDetail";
    }


    //跳转到回答界面
    @RequireLogin
    @RequestMapping("/commentAdd")
    public String commentAdd(Model model,@UserParam UserInfo userInfo, int floor, ProblemComment comment){

        //redis增加回答数
        userInfoReplyVOService.addReplySorce(userInfo.getId(),1);

        //增加回答数
        problemStatisVOService.increaseReplynum(comment.getProblemId(),1);
        //设置用户的信息
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment = problemCommentService.save(comment);

        //c
        model.addAttribute("c",comment);
        //floor
        model.addAttribute("floor",problemCommentService.getCount(comment.getProblemId()));



        return "problem/commentTpl";
    }


    //关注
    @RequestMapping("/focus")
    @ResponseBody
    @RequireLogin
    public AjaxResult focus(Long cid,@UserParam UserInfo userInfo){
        //cid是问题作者id
        //用当前登录用户,关注传过来的作者id
        Long uid = userInfo.getId();
        boolean b = userInfoService.fans(cid,uid);
        if(b){
            return new AjaxResult(b);
        }else{
            return new AjaxResult(b,"已经关注过");
        }
    }

    //顶
    @RequestMapping("/zan")
    @ResponseBody
    @RequireLogin
    public AjaxResult zan(Long pid){

        problemStatisVOService.increaseThumbsupnum(pid,1);

        Problem p = problemService.getById(pid);
        //点赞时给用户加分
        userInfoStaticVOService.addZanSorce(p.getUser().getId(),1);

        return new AjaxResult(true);
    }
}
